package com.software.sqlite.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.software.sqlite.api.admin.v1.vo.AdminLoginUserVO;
import com.software.sqlite.common.BaseMapper;
import com.software.sqlite.common.CommonContant;
import com.software.sqlite.entity.Page;
import com.software.sqlite.service.IBaseService;
import com.software.sqlite.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.ParameterizedType;
import java.util.List;
@Service
@Slf4j
@Transactional
public abstract class BaseService<T, ID, EXAMPLE> implements IBaseService<T, ID, EXAMPLE> {

    // 定义抽象方法getMyMapper获取当前实体Mapper对象
    protected abstract BaseMapper<T> getBaseMapper();
    @Resource
    private HttpServletRequest request;

    private Class<T> modelClass;//当前泛型的真实类型Class
    public BaseService() {
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        modelClass = (Class<T>) pt.getActualTypeArguments()[0];
    }

    public AdminLoginUserVO getLoginUser(String token)
    {
        HttpSession session = request.getSession();
        AdminLoginUserVO loginUser = (AdminLoginUserVO) session.getAttribute(CommonContant.ADMIN_LOGIN_SESSION);
        if(!(loginUser!=null && loginUser.getToken().equals(token)))
        {
            loginUser=null;
        }
        return loginUser;
    }

    public AdminLoginUserVO getAppLoginUser(String token)
    {
        HttpSession session = request.getSession();
        AdminLoginUserVO loginUser = (AdminLoginUserVO) session.getAttribute(CommonContant.APP_LOGIN_SESSION);
        if(!(loginUser!=null && loginUser.getToken().equals(token)))
        {
            loginUser=null;
        }
        return loginUser;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<T> selectList(T entity) throws Exception {
        return getBaseMapper().select(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<T> selectListBySQL(T entity) throws Exception {
        return getBaseMapper().selectListBySQL(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page selectPage(Integer pageNo, Integer pageSize, T entity) throws Exception {
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=10;
        }
        Page page=new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        if(entity!=null){
            PageHelper.startPage(pageNo, pageSize);
            PageInfo<T> pageInfo = new PageInfo<T>(selectListBySQL(entity));
            page.setList(pageInfo.getList());
            page.setTotalSize(Integer.parseInt(pageInfo.getTotal()+""));
            page.setTotalPage(pageInfo.getPages());
        }
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public T get(Long id) throws Exception {
        return (T)getBaseMapper().selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public T get(T entity) throws Exception {
        return (T)getBaseMapper().select(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(T entity) throws Exception {
        getBaseMapper().insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(T entity) throws Exception {
        getBaseMapper().updateByPrimaryKeySelective(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(Long id) throws Exception {
        getBaseMapper().deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addList(List<T> list) throws Exception {
        getBaseMapper().insertList(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateList(List<T> entity) throws Exception {
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delList(Long[] ids) throws Exception {
        if(ids!=null){
            for(Long id:ids){
                getBaseMapper().deleteByPrimaryKey(id);
            }
        }
    }
}
